package offer;

public class offer67 {
    public static int strToInt(String str) {
        str = str.trim();
//        先判断是否有正负号
        int index ;
        int end ;
        if(str.startsWith("+"))
        {
            index =0;
            end =1;
            for (int i = index+1; i <str.length() ; i++) {
                String tmp = str.substring(i,str.length());
                if(tmp.startsWith("0-9"))
                {
                    end++;
                }
                else break;
            }
            if(end==1) return 0;
        }else if(str.startsWith("-"))
        {
            index =0;
            end = 1;
            for (int i = index+1; i <str.length() ; i++) {
                String tmp = str.substring(i,str.length());
                if(tmp.startsWith("0-9"))
                {
                    end++;
                }
                else break;
            }
            if(end==1) return 0;
        }else {
            index =0;
            end =0;
            for (int i = index; i < str.length(); i++) {
                String tmp = str.substring(i,str.length());
                if(tmp.startsWith("0-9"))
                {
                    end++;
                }
                else
                    break;
            }
            if(end==0) return 0;
        }
        String res = str.substring(index,end);
        long ans = Long.parseLong(res);
        int gg=0 ;
        if(ans<=-2147483648)
            gg =-2147483648;
        if(ans>2147483647)
            gg =2147483647;
        else if(ans>-2147483648&&ans<=2147483647) {
            gg = (int)ans;
        }

        return gg;
    }

    public static void main(String[] args) {
      String str= "20000000000000000000";
      strToInt(str);
    }
}
